#include<stdio.h>
#include<stdlib.h>
#include<string.h>

    typedef struct gps_data
    {
		long GPSDATA_ID;
		char GPSTIME[9];
		long DEVID;
		double LATITUDE;
		double LONGTITUDE;
		int FLAG;
		int TRIP_ID;
		int TRIP_ID_X;
		int TRIP_ID_Y;
	}DATA;

int main()
{
	char my_cmd[80];
	char file_name[20],file_path[100],file1[100],file2[100],file3[100];
	FILE *fp,*fp2,*fp3;
	int i = 0, k, j, l = 0;
	DATA data[2];
	char str[20]={0};
	char state[10]={0};
	int TIME_FLAG[2];
	int count[30][30][48][2]={0};//对不同时段不同栅格计数


	printf("请输入原始数据文件夹:(注意使用'\\')\n");
	scanf("%s",file1);
	sprintf(my_cmd, "DIR/B/A-D %s >> filename.txt",file1);
	printf("请输入输出数据文件夹(注意使用'\\\\')\n");
	scanf("%s",file2);

	system(my_cmd);
	if ((fp = fopen("filename.txt","r"))==NULL)
	{
		printf("Faliure to open filename.txt!\n");
		exit(0);
	}

ff1:switch(str[0]=fgetc(fp))
	{
	case EOF:
		goto ff2;
	default :
		break;
	}
	
	j = 1;
	do
	{
		str[j]=fgetc(fp);
		j++;
	}while(str[j-1] != '\n');
	str[j-1] = 0;
	strcpy(file_name, str);

	sprintf(file_path, "%s\\%s",file1,file_name);

	if ((fp2 = fopen(file_path,"r"))==NULL)
	{
		printf("Failure to open file!\n");
		exit(0);
	}

			
	for (k = 0;; k++)
	{
	ff3:switch(str[0]=fgetc(fp2))
		{
		case EOF:
			fclose(fp2);
			goto ff1;
		default :
			break;
		}
		
		j = 1;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != ',');
		str[j-1] = 0;//逗号清零
		data[i].GPSDATA_ID = atoi(str);//字符串转换成int，读取GPSDATA_ID
		strcpy(str,"");//str清空

		fgets(str,9,fp2);
		strcpy(data[i].GPSTIME, str);//读取GPSTIME
		str[0]=fgetc(fp2);//跳过','
		strcpy(str,"");

		if((data[i].GPSTIME[3] - 48) < 3)
		{
			TIME_FLAG[i] = (data[i].GPSTIME[0] - 48) * 20 + (data[i].GPSTIME[1] - 48) * 2 + 1;
		}
		else TIME_FLAG[i] = (data[i].GPSTIME[0] - 48) * 20 + (data[i].GPSTIME[1] - 48) * 2 + 2;

		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != ',');
		str[j-1] = 0;
		data[i].DEVID = atoi(str);//读取DEVID
		strcpy(str,"");

		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != ',');
		str[j-1] = 0;
		data[i].LATITUDE = atof(str);//读取LATITUDE
		strcpy(str,"");

		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != ',');
		data[i].LONGTITUDE = atof(str);//读取LONGTITUDE
		strcpy(str,"");
		
		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != '.');
		data[i].FLAG = atoi(str);//读取FLAG
		strcpy(str,"");

		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != ',');
		data[i].TRIP_ID = atoi(str);//读取TRIP_ID
		strcpy(str,"");
		
		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != '-');
		data[i].TRIP_ID_X = atoi(str);//读取TRIP_ID_X
		strcpy(str,"");

		j = 0;
		do
		{
			str[j]=fgetc(fp2);
			j++;
		}while(str[j-1] != '\n');
		data[i].TRIP_ID_Y = atoi(str);//读取TRIP_ID_Y
		strcpy(str,"");
		
		printf("%s %d\n",file_name,k+1);
		if(data[i].TRIP_ID_X > 29 || data[i].TRIP_ID_Y > 29)
		{
			goto ff3;
		}

		if(k==0)
		{
			data[1-i].FLAG = data[i].FLAG;
		}
		
		if(data[1-i].FLAG != data[i].FLAG)
		{
			count[data[1-i].TRIP_ID_X][data[1-i].TRIP_ID_Y][TIME_FLAG[1-i]-1][data[1-i].FLAG]++;
		}
				
		i = 1 - i;

	}
ff2:fclose(fp);

	for(i = 0; i < 48; i++)//时段T的循环
	{
		for(j = 0; j < 2; j++)//上下客0/1循环
		{
			sprintf(file3,"%s\\%d-%d.txt",file2,j,i+1);
			if((fp3=fopen(file3,"at+"))==NULL)
			{
				printf("failure to save!\n");
				exit(0);
			}

			for(k = 0; k < 30; k++)//Y坐标循环
			{
				for(l = 0; l < 30; l++)//X坐标循环
				{
					fprintf(fp3,"%d,%d\n",l+30*k,count[l][k][i][j]);
				}
			}
			fclose(fp3);
		}
	}




	system("pause");
}